package com.zw.mes.module.tecsummary.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.technology.*;
import com.zw.mes.module.cost.model.DictCommon;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.tecsummary.service.SummaryService;
import com.zw.mes.util.FileUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;
import static com.zw.mes.constant.Constant.SUMMARY_INTO;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName Summary.java
 * @Description TODO
 * @createTime 2021年03月29日 09:46:00
 */
@Controller
@SuppressWarnings("all")
@RequestMapping("/summary")
public class SummaryController extends BaseController {
    @Autowired
    private SummaryService summaryService1;
    @Autowired
    private DictCommonService dictCommonService;


    /**
     * @Author mayongfeng
     * @Date 14:15 2021/1/29
     * @Description 页面跳转到外购件计划分配
     **/
    @GetMapping
    public String listPage(Model model, Result result) {
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "technology/summary/selectAll";
    }

    @PostMapping("/into")
    @ResponseBody
    private JsonResult into(Integer cardStatus, Integer makeStatus, Integer outStatus,Integer paintStatus,String proCode,String special,String part) {
        List<DictCommon> dictCommons = dictCommonService.findDictByGroupNameAndSummary(SUMMARY_INTO, cardStatus, makeStatus, outStatus,paintStatus,proCode,special,part);
        return jsonResult(dictCommons);
    }

    /**
     * @Author mayongfeng
     * @Date 14:28 2021/5/6
     * @Description 查询技术汇总列表页面
     * 该列表查询就是技术资料汇总查询加载页面，只要在生成系统中，有关（外购件、制作件、涂装设计、工艺卡接收）就会另存
     * 当前表中，然后在折贴展开时，通过查询出来的，状态判断当前是否有列表数据生成
     **/
    @PostMapping("/list")
    @ResponseBody
    private JsonResult list(ReceiveSum receiveSum) {
        List<ReceiveSum> receiveSumList = summaryService1.findList(receiveSum);
        return jsonResult(receiveSumList);
    }

    /**
     * @Author mayongfeng
     * @Date 14:28 2021/5/6
     * @Description 查询技术汇总列表页面
     **/
    @GetMapping("/typeList")
    private String typeList(String proCode, String special, String part, Integer typeId, Model model) {
        /** 判断查看得类型，51=提报计划，52=制作件清单，53=图纸变更单，54=涂装设计清单，55=工艺卡列表，56=工艺路线，57=机加工时，58=机加工时汇总*/
        if (typeId == 51) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/repage";
        } else if (typeId == 52) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/makeQuery";
        } else if (typeId == 53) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/drawQuery";
        } else if (typeId == 54) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/coatQuery";
        } else if (typeId == 55) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/craCard";
        } else if (typeId == 56) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/craRoute";
        } else if (typeId == 57) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/machin";
        } else if (typeId == 58) {
            JumpPass(proCode, special, part, typeId, model);
            return "technology/summary/sumMachin";
        }

        return "technology/make/listPage";
    }

    private void JumpPass(String proCode, String special, String part, Integer typeId, Model model) {
        model.addAttribute("proCode", proCode);
        model.addAttribute("special", special);
        model.addAttribute("part", part);
        model.addAttribute("typeId", typeId);
    }


    /**
     * 提报计划列表
     */
    @ResponseBody
    @PostMapping("report/query/list")
    public JsonResult queryList(Result result) {
        startPage();
        List<Result> list = summaryService1.reportSee(result);
        return jsonResult(list);
    }

    /**
     * 制作件清单查询
     */
    @ResponseBody
    @PostMapping("make/query/list")
    public JsonResult makeList(MakeResult makeResult) {
        startPage();
        List<MakeResult> list = summaryService1.makeSee(makeResult);
        return jsonResult(list);
    }
    /**
     * 图纸变更单查询
     */
    @ResponseBody
    @PostMapping("drawChange/query/list")
    public JsonResult drawChangeList(DrawSheet drawSheet) {
        startPage();
        List<MakeResult> list = summaryService1.drawSee(drawSheet);
        return jsonResult(list);
    }
    /**
     * 图装设计清单
     */
    @ResponseBody
    @PostMapping("coat/queryList")
    public JsonResult coatQueryList(DsnCoatSheet dsnCoatSheet) {
        startPage();
        List<DsnCoatSheet> list = summaryService1.paintSee(dsnCoatSheet);
        return jsonResult(list);
    }



    /**
     * 工艺卡查询
     */
    @ResponseBody
    @PostMapping("cardQuery/list")
    public JsonResult cardQueryList(Tecard tecard) {
        startPage();
        List<Tecard> list = summaryService1.cardSee(tecard);
        return jsonResult(list);
    }

    /**
     * 工艺路线查询
     */
    @ResponseBody
    @PostMapping("craRoute/list")
    public JsonResult cardRouteList(Tecard tecard) {
        startPage();
        List<Tecard> list = summaryService1.routeSee(tecard);
        return jsonResult(list);
    }

     /**
     * 机加工时查询
     */
    @ResponseBody
    @PostMapping("machin/list")
    public JsonResult machinList(Tecard tecard) {
        startPage();
        List<Tecard> list = summaryService1.machHoursSee(tecard);
        return jsonResult(list);
    }

    /**
     * 机加工时汇总查询
     */
    @ResponseBody
    @PostMapping("sumMachin/list")
    public JsonResult sumMachinList(Tecard tecard) {
        startPage();
        List<Tecard> list = summaryService1.summaryHoursSee(tecard);
        return jsonResult(list);
    }

    /**
     * @Author mayongfeng
     * @Date 10:09 2021/2/20
     * @Description  图纸变更单明细查询
     *
     **/
    @GetMapping("selectDraw")
    public String selectDetail(String id, Model model){
        /** 通过Id查询该项目下的标头*/
        model.addAttribute("drawSheet", summaryService1.getDrawSheet(id));
        /** 查询明细*/
        model.addAttribute("drawChange", summaryService1.getDrawDetail(id));
        return "technology/summary/drawPlan";
    }

    /**
     * 图纸查看附件查询路径
     */
    @GetMapping("downloadProjectFileDraw/{id}")
    public void downloadProjectFileDraw(@PathVariable("id") Integer id, HttpServletResponse httpServletResponse){
        try{
            DrawSheet drawSheet = summaryService1.getDrawUrlDetail(id);
            String filename = drawSheet.getUrl();
            filename = filename.replace("/files/", "e:/pdm/upload/");
            FileUtils.downloadFile(filename,httpServletResponse);
        }catch (Exception exception){
            exception.printStackTrace();
        }
    }

}
